package com.openfarmanager.android.core;

import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;
import com.openfarmanager.android.App;
import com.openfarmanager.android.filesystem.FileSystemScanner;
import com.openfarmanager.android.utils.SystemUtils;

import java.io.File;

public class Settings {

    public static final String PANEL_STATE_SETTINGS = "panelState";
    public static final String LEFT_PANEL_PATH = "left";
    public static final String RIGHT_PANEL_PATH = "right";
    public static final String ACTIVE_PANEL = "active_panel";

    public static final String SDCARD_ROOT = "sdcard_root";
    public static final String HIDE_SYSTEM_FILES = "hide_system_files";
    public static final String FILES_SORT = "files_sort";
    public static final String FOLDERS_FIRST = "folders_first";
    public static final String MULTI_PANELS = "multi_panels";
    public static final String MULTI_PANELS_CHANGED = "multi_panels_changed";

    private static File sSdCard;
    public static String sSdPath;

    static {
        sSdCard = Environment.getExternalStorageDirectory();
        sSdPath = sSdCard != null ? sSdCard.getPath() : FileSystemScanner.ROOT;
    }

    public void savePanelsState(String leftPanelPath, String rightPanelPath, boolean isLeftPanelActive) {
        SharedPreferences.Editor edit = getPanelSettings().edit();
        edit.putString(LEFT_PANEL_PATH, leftPanelPath);
        edit.putString(RIGHT_PANEL_PATH, rightPanelPath);
        edit.putBoolean(ACTIVE_PANEL, isLeftPanelActive);
        edit.commit();
    }

    public SharedPreferences getPanelSettings() {
        return App.sInstance.getSharedPreferences(PANEL_STATE_SETTINGS, 0);
    }

    public String getLeftPanelPath() {
        return getPanelSettings().getString(LEFT_PANEL_PATH, sSdPath);
    }

    public String getRightPanelPath() {
        return getPanelSettings().getString(RIGHT_PANEL_PATH, sSdPath);
    }

    public boolean isLeftPanelActive() {
        return getPanelSettings().getBoolean(ACTIVE_PANEL, true);
    }

    public SharedPreferences getSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(App.sInstance);
    }

    public boolean isSDCardRoot() {
        return getSharedPreferences().getBoolean(SDCARD_ROOT, false);
    }

    public boolean isHideSystemFiles() {
        return getSharedPreferences().getBoolean(HIDE_SYSTEM_FILES, false);
    }

    public boolean isFoldersFirst() {
        return getSharedPreferences().getBoolean(FOLDERS_FIRST, false);
    }

    public String getFileSortValue() {
        return getSharedPreferences().getString(FILES_SORT, "0");
    }

    public void setFileSortValue(String value) {
        getSharedPreferences().edit().putString(Settings.FILES_SORT, value).commit();
        FileSystemScanner.sInstance.initFilters();
    }

    public boolean isMultiPanelMode() {
        if (!getSharedPreferences().getBoolean(Settings.MULTI_PANELS_CHANGED, false)) {
            return SystemUtils.isTablet();
        }

        return getSharedPreferences().getBoolean(MULTI_PANELS, SystemUtils.isTablet());
    }

}
